
layui.use(['layer', 'form', 'admin', 'laydate', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;


    // 添加表单验证方法
    form.verify({
        sort: [/^[\d]{1,3}$/, '排序号为1-3位正整数'],
        score: [/^[\d]{1,2}([\.][0-9])?$/, '分数为1-2位整数或小数']
    });

    //获取部门option
    var deptList = [];
    var deptAjax = new $ax(Feng.ctxPath + "/dept/selectChildren",function (data) {
        deptList = data;
    }, function (data) {
        Feng.error("获取部门失败！" + data.responseJSON.message)
    });
    deptAjax.start();
    var deptOption = "<option value=''>选择部门</option>";
    for(var i=0;i<deptList.length;i++){
        deptOption = deptOption + "<option value='"+deptList[i].dept_id+"'>"+deptList[i].simple_name+"</option>";
    }

    var regionType = $("#regionType").val();
    var levelId = $("#levelId").val();
    var ajax = new $ax(Feng.ctxPath + "/content/listContentByLevelId");
    ajax.set("levelId",levelId);
    ajax.set("regionType",regionType);
    var result = ajax.start();
    if(result==null || result.length ==0){
        //数据为空。初始化操作表
        var table = '<table class="layui-table" style="text-align: center;">' +
            '            <tr>' +
            '                <td colspan="9">' +
            '                    <label class="layui-form-label" style="width: 65px;padding-left: 0px;">工作内容</label>' +
            '                    <div class="layui-input-inline" style="width: 275px;">' +
            '                        <input type="hidden" name="contentId" />'+
            '                        <input name="content" maxlength="50" placeholder="请输入工作内容" style="width: 274px;" type="text" class="layui-input"' +
            '                               />' +
            '                    </div>' +
            '                </td>' +
            '                <td rowspan="100" width="30" style="cursor: pointer;">' +
            '                    <span  class="layui-badge layui-bg-blue addTable">+</span >' +
            '                </td>' +
            '            </tr>' +
            '            <tr>' +
            '                <td width="200">资料名称<span style="color: red;">*</span></td>' +
            '                <td width="110">类型<span style="color: red;">*</span></td>' +
            '                <td width="70">基础分<span style="color: red;">*</span></td>' +
            '                <td width="70">评判分<span style="color: red;">*</span></td>' +
            '                <td width="70">提报时间<span style="color: red;">*</span></td>' +
            '                <td width="60">排序<span style="color: red;">*</span></td>' +
            '                <td width="100">部门<span style="color: red;">*</span></td>' +
            '                <td rowspan="2" width="20" style="cursor: pointer;"><span  class="layui-badge layui-bg-blue addRow">+</span ></td>' +
            '            </tr>' +
            '            <tr class="dataTr">' +
            '                <td>' +
            '                    <input type="hidden" name="dataId"/>' +
            '                    <input name="dataName" maxlength="200" placeholder="请输入资料名称" type="text" class="layui-input priceType"' +
            '                           onblur="Feng.trim(this);" lay-verify="required" required/>' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataType" lay-verify="required" required>' +
            '                        <option value="">请选择类型</option>' +
            '                        <option value="word">word</option>' +
            '                        <option value="excel">excel</option>' +
            '                        <option value="pdf">pdf</option>' +
            '                        <option value="img">img</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="baseScore" maxlength="2" placeholder="基础分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <input name="dataScore" maxlength="2" placeholder="评判分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataMonth" lay-verify="required" required>' +
            '                        <option value="1">1月</option>' +
            '                        <option value="2">2月</option>' +
            '                        <option value="3">3月</option>' +
            '                        <option value="4">4月</option>' +
            '                        <option value="5">5月</option>' +
            '                        <option value="6">6月</option>' +
            '                        <option value="7">7月</option>' +
            '                        <option value="8">8月</option>' +
            '                        <option value="9">9月</option>' +
            '                        <option value="10">10月</option>' +
            '                        <option value="11">11月</option>' +
            '                        <option value="12">12月</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="sort" maxlength="3" placeholder="排序" type="number" value="1" class="layui-input"' +
            '                           lay-verify="sort" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dept" lay-verify="required" required>' +
                                    deptOption +
            '                    </select>' +
            '                </td>' +
            '            </tr>' +
            '        </table>';
        $(".table_container").append(table);
        form.render("select")
        // 让当前iframe弹层高度适应
        admin.iframeAuto();
    }else{
        for(var i=0;i<result.length;i++){
            var datas = result[i].dataList;
            var trs = "";
            for(var j=0;j<datas.length;j++){
                var data = datas[j];
                var trOp = "";
                if(j>0){
                    trOp = '<td width="20" style="cursor: pointer;"><span  class="layui-badge delRow">-</span ></td>';
                }
                var trDeptOption = "<option value=''>选择部门</option>";
                for(var k=0;k<deptList.length;k++){
                    if(data.deptId == deptList[k].dept_id){
                        trDeptOption += "<option value='"+deptList[k].dept_id+"' selected>"+deptList[k].simple_name+"</option>";
                    }else{
                        trDeptOption += "<option value='"+deptList[k].dept_id+"'>"+deptList[k].simple_name+"</option>";
                    }
                }
                var tr = '            <tr class="dataTr">' +
                    '                <td>' +
                    '                    <input type="hidden" name="dataId" value="'+data.dataId+'"/>'+
                    '                    <input name="dataName" value="'+data.dataName+'" maxlength="200" placeholder="请输入资料名称" type="text" class="layui-input priceType"' +
                    '                           onblur="Feng.trim(this);" lay-verify="required" required/>' +
                    '                </td>' +
                    '                <td>' +
                    '                    <select name="dataType"  lay-verify="required" required>' +
                    '                        <option value="">请选择类型</option>' +
                    '                        <option '+(data.dataType=='word'?'selected':'') + ' value="word">word</option>' +
                    '                        <option '+(data.dataType=='excel'?'selected':'') + ' value="excel">excel</option>' +
                    '                        <option '+(data.dataType=='pdf'?'selected':'') + ' value="pdf">pdf</option>' +
                    '                        <option '+(data.dataType=='img'?'selected':'') + ' value="img">img</option>' +
                    '                    </select>' +
                    '                </td>' +
                    '                <td>' +
                    '                    <input name="baseScore" value="'+data.baseScore+'" maxlength="2" placeholder="基础分" type="number" step="0.1" class="layui-input"' +
                    '                           lay-verify="score" required />' +
                    '                </td>' +
                    '                <td>' +
                    '                    <input name="dataScore" value="'+data.dataScore+'" maxlength="2" placeholder="评判分" type="number" step="0.1" class="layui-input"' +
                    '                           lay-verify="score" required />' +
                    '                </td>' +
                    '                <td>' +
                    '                    <select name="dataMonth" value="'+data.dataMonth+'" lay-verify="required" required>' +
                    '                        <option '+ (data.dataMonth==1?'selected':'') +' value="1">1月</option>' +
                    '                        <option '+ (data.dataMonth==2?'selected':'') +' value="2">2月</option>' +
                    '                        <option '+ (data.dataMonth==3?'selected':'') +' value="3">3月</option>' +
                    '                        <option '+ (data.dataMonth==4?'selected':'') +' value="4">4月</option>' +
                    '                        <option '+ (data.dataMonth==5?'selected':'') +' value="5">5月</option>' +
                    '                        <option '+ (data.dataMonth==6?'selected':'') +' value="6">6月</option>' +
                    '                        <option '+ (data.dataMonth==7?'selected':'') +' value="7">7月</option>' +
                    '                        <option '+ (data.dataMonth==8?'selected':'') +' value="8">8月</option>' +
                    '                        <option '+ (data.dataMonth==9?'selected':'') +' value="9">9月</option>' +
                    '                        <option '+ (data.dataMonth==10?'selected':'') +' value="10">10月</option>' +
                    '                        <option '+ (data.dataMonth==11?'selected':'') +' value="11">11月</option>' +
                    '                        <option '+ (data.dataMonth==12?'selected':'') +' value="12">12月</option>' +
                    '                    </select>' +
                    '                </td>' +
                    '                <td>' +
                    '                    <input name="sort" value="'+data.sort+'" maxlength="3" placeholder="排序" type="number" value="1" class="layui-input"' +
                    '                           lay-verify="sort" required />' +
                    '                </td>' +
                    '                <td>' +
                    '                    <select name="dept" lay-verify="required" required>' +
                                                trDeptOption +
                    '                    </select>' +
                    '                </td>' +
                                    trOp +
                    '            </tr>' ;
                trs = trs + tr;
            }


            var tableOp = '<span  class="layui-badge delTable">-</span >';
            if(i==0){
                tableOp = '<span  class="layui-badge layui-bg-blue addTable">+</span >';
            }

            var table = '<table class="layui-table" style="text-align: center;">' +
                '            <tr>' +
                '                <td colspan="9">' +
                '                    <label class="layui-form-label" style="width: 65px;padding-left: 0px;">工作内容</label>' +
                '                    <div class="layui-input-inline" style="width: 275px;">' +
                '                        <input type="hidden" name="contentId" value="'+result[i].contentId+'"/>'+
                '                        <input name="content" value="'+result[i].content+'" maxlength="50" placeholder="请输入工作内容" style="width: 274px;" type="text" class="layui-input"' +
                '                               />' +
                '                    </div>' +
                '                </td>' +
                '                <td rowspan="100" width="30" style="cursor: pointer;">' +
                tableOp +
                '                </td>' +
                '            </tr>' +
                '            <tr>' +
                '                <td width="200">资料名称<span style="color: red;">*</span></td>' +
                '                <td width="110">类型<span style="color: red;">*</span></td>' +
                '                <td width="70">基础分<span style="color: red;">*</span></td>' +
                '                <td width="70">评判分<span style="color: red;">*</span></td>' +
                '                <td width="70">提报时间<span style="color: red;">*</span></td>' +
                '                <td width="60">排序<span style="color: red;">*</span></td>' +
                '                <td width="100">部门<span style="color: red;">*</span></td>' +
                '                <td rowspan="2" width="20" style="cursor: pointer;"><span  class="layui-badge layui-bg-blue addRow">+</span ></td>' +
                '            </tr>' +
                trs +
                '        </table>';
            $(".table_container").append(table)
            form.render("select")
            // 让当前iframe弹层高度适应
            admin.iframeAuto();
        }
    }




    $(document).on("click",".addRow",function () {
        var row = '<tr class="dataTr">' +
            '                <td>' +
            '                    <input type="hidden" name="dataId" value=""/>'+
            '                    <input name="dataName" maxlength="200" placeholder="请输入资料名称" type="text" class="layui-input priceType"' +
            '                           onblur="Feng.trim(this);" lay-verify="required" required/>' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataType" lay-verify="required" required>' +
            '                        <option value="">请选择类型</option>' +
            '                        <option value="word">word</option>' +
            '                        <option value="excel">excel</option>' +
            '                        <option value="pdf">pdf</option>' +
            '                        <option value="img">img</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="baseScore" maxlength="2" placeholder="基础分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <input name="dataScore" maxlength="2" placeholder="评判分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataMonth" lay-verify="required" required>' +
            '                        <option value="1">1月</option>' +
            '                        <option value="2">2月</option>' +
            '                        <option value="3">3月</option>' +
            '                        <option value="4">4月</option>' +
            '                        <option value="5">5月</option>' +
            '                        <option value="6">6月</option>' +
            '                        <option value="7">7月</option>' +
            '                        <option value="8">8月</option>' +
            '                        <option value="9">9月</option>' +
            '                        <option value="10">10月</option>' +
            '                        <option value="11">11月</option>' +
            '                        <option value="12">12月</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="sort" maxlength="3" placeholder="排序" type="number" value="1" class="layui-input"' +
            '                           lay-verify="sort" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dept" lay-verify="required" required>' +
                                    deptOption +
            '                    </select>' +
            '                </td>' +
            '                <td width="20" style="cursor: pointer;"><span  class="layui-badge delRow">-</span ></td>'+
            '         </tr>'
        $(this).parents('table').append(row);
        form.render("select")
        // 让当前iframe弹层高度适应
        admin.iframeAuto();
    })

    $(document).on("click",".delRow",function () {
        var dataId = $(this).parents("tr").find("input[name='dataId']").val().trim();
        var that = $(this);
        if (dataId != null && dataId != '' && dataId != undefined) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/content/deleteData", function () {
                    that.parents("tr").remove()
                    Feng.success("删除成功!");
                }, function (data) {
                    Feng.error("删除资料失败！" + data.responseJSON.message)
                });
                ajax.set("dataId", dataId);
                ajax.start();
            };
            Feng.confirm('是否删除资料?删除后不可恢复', operation);
        }else{
            $(this).parents("tr").remove();
        }
        // 让当前iframe弹层高度适应
        admin.iframeAuto();
    })

    $(document).on("click",".addTable",function () {
        var table = '<table class="layui-table" style="text-align: center;">' +
            '            <tr>' +
            '                <td colspan="9">' +
            '                    <label class="layui-form-label" style="width: 65px;padding-left: 0px;">工作内容</label>' +
            '                    <div class="layui-input-inline" style="width: 275px;">' +
            '                        <input type="hidden" name="contentId" />'+
            '                        <input name="content" maxlength="50" placeholder="请输入工作内容" style="width: 274px;" type="text" class="layui-input"' +
            '                               />' +
            '                    </div>' +
            '                </td>' +
            '                <td rowspan="100" width="30" style="cursor: pointer;">' +
            '                    <span  class="layui-badge delTable">-</span >' +
            '                </td>' +
            '            </tr>' +
            '            <tr>' +
            '                <td width="200">资料名称<span style="color: red;">*</span></td>' +
            '                <td width="110">类型<span style="color: red;">*</span></td>' +
            '                <td width="70">基础分<span style="color: red;">*</span></td>' +
            '                <td width="70">评判分<span style="color: red;">*</span></td>' +
            '                <td width="70">提报时间<span style="color: red;">*</span></td>' +
            '                <td width="60">排序<span style="color: red;">*</span></td>' +
            '                <td width="100">部门<span style="color: red;">*</span></td>' +
            '                <td rowspan="2" width="20" style="cursor: pointer;"><span  class="layui-badge layui-bg-blue addRow">+</span ></td>' +
            '            </tr>' +
            '            <tr class="dataTr">' +
            '                <td>' +
            '                    <input type="hidden" name="dataId" value=""/>'+
            '                    <input name="dataName" maxlength="200" placeholder="请输入资料名称" type="text" class="layui-input priceType"' +
            '                           onblur="Feng.trim(this);" lay-verify="required" required/>' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataType" lay-verify="required" required>' +
            '                        <option value="">请选择类型</option>' +
            '                        <option value="word">word</option>' +
            '                        <option value="excel">excel</option>' +
            '                        <option value="pdf">pdf</option>' +
            '                        <option value="img">img</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="baseScore" maxlength="2" placeholder="基础分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <input name="dataScore" maxlength="2" placeholder="评判分" type="number" step="0.1" class="layui-input"' +
            '                           lay-verify="score" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dataMonth" lay-verify="required" required>' +
            '                        <option value="1">1月</option>' +
            '                        <option value="2">2月</option>' +
            '                        <option value="3">3月</option>' +
            '                        <option value="4">4月</option>' +
            '                        <option value="5">5月</option>' +
            '                        <option value="6">6月</option>' +
            '                        <option value="7">7月</option>' +
            '                        <option value="8">8月</option>' +
            '                        <option value="9">9月</option>' +
            '                        <option value="10">10月</option>' +
            '                        <option value="11">11月</option>' +
            '                        <option value="12">12月</option>' +
            '                    </select>' +
            '                </td>' +
            '                <td>' +
            '                    <input name="sort" maxlength="3" placeholder="排序" type="number" value="1" class="layui-input"' +
            '                           lay-verify="sort" required />' +
            '                </td>' +
            '                <td>' +
            '                    <select name="dept" lay-verify="required" required>' +
                                    deptOption +
            '                    </select>' +
            '                </td>' +
            '            </tr>' +
            '        </table>';
        $(".table_container").append(table);
        $(document).scrollTop($(document).height());
        form.render("select")
        // 让当前iframe弹层高度适应
        admin.iframeAuto();
    })

    $(document).on("click",".delTable",function () {
        var contentId = $(this).parents("table").find("input[name='contentId']").val().trim();
        var that = $(this);
        if (contentId != null && contentId != '' && contentId != undefined) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/content/deleteContent", function () {
                    that.parents("table").remove();
                    Feng.success("删除成功!");
                }, function (data) {
                    Feng.error("删除内容失败！" + data.responseJSON.message)
                });
                ajax.set("contentId", contentId);
                ajax.set("levelId", $("#levelId").val());
                ajax.start();
            };
            Feng.confirm('是否删除内容及资料?删除后不可恢复', operation);
        }else{
            $(this).parents("table").remove();
        }
        // 让当前iframe弹层高度适应
        admin.iframeAuto();
    })

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/content/edit", function (data) {
            Feng.success("编辑成功！")
        }, function (data) {
            Feng.error("编辑失败！" + data.responseJSON.message)
        });
        var contentArr = new Array();
        var regionType = $("#regionType").val();
        var levelId = $("#levelId").val();
        var year = $("#year").val();
        $("table").each(function (index,ele) {
            var content = new Object();
            content.contentId = $(ele).find("input[name='contentId']").val();
            content.content = $(ele).find("input[name='content']").val().trim();
            content.order = index+1;
            content.regionType = regionType;
            content.levelId = levelId;
            content.year = year;
            var dataArr = new Array();
            $(ele).find(".dataTr").each(function (index,ele) {
                var data = new Object();
                data.dataId =  $(ele).find("input[name='dataId']").val();
                data.regionType = regionType;
                data.dataName = $(ele).find("input[name='dataName']").val();
                data.dataType = $(ele).find("select[name='dataType']").val();
                data.dataYear = year;
                data.baseScore = $(ele).find("input[name='baseScore']").val();
                data.dataScore = $(ele).find("input[name='dataScore']").val();
                data.dataMonth = $(ele).find("select[name='dataMonth']").val();
                data.sort = $(ele).find("input[name='sort']").val();
                data.dept = $(ele).find("select[name='dept']").val();
                dataArr.push(data)
            })
            content.datas = dataArr;
            contentArr.push(content)
        })
        ajax.set(data.field)
        ajax.set("contentJson",JSON.stringify(contentArr));
        ajax.start();
    });
});